﻿
using System.Threading.Tasks;
namespace WeiboWPSdk {
    public class ClientHttpHelper {
        public ClientHttpHelper(OAuth2 oauth) {
            OAuth2 = oauth;
        }

        public readonly OAuth2 OAuth2;

        private async Task<string> HttpAsync(string cmd, HttpRequestMethod method, string baseUrl, params WeiboParameter[] parameters) {
            string url = string.Empty;
            if (cmd.StartsWith("http://") || cmd.StartsWith("https://"))
                url = cmd;
            else
                url = string.Format("{0}{1}.json", baseUrl, cmd);
            return await OAuth2.RequestAsync(url, method, parameters);
        }
        /// <summary>
        /// GET方式发送基础微博指令
        /// </summary>
        /// <param name="command">微博命令。如statuses/public_timeline。详见官方API文档：http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2 </param>
        /// <param name="parameters">参数表</param>
        /// <returns></returns>
        internal async Task<string> GetBasicCommandAsync(string command, params WeiboParameter[] parameters) {
            return await HttpAsync(command, HttpRequestMethod.Get, Properties.UrlResource.BaseUrl, parameters);
        }
        /// <summary>
        /// POST方式发送基础微博指令
        /// </summary>
        /// <param name="command">微博命令。如comments/create。详见官方API文档：http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2 </param>
        /// <param name="parameters">参数表</param>
        /// <returns></returns>
        internal async Task<string> PostBasicCommandAsync(string command, params WeiboParameter[] parameters) {
            return await HttpAsync(command, HttpRequestMethod.Post, Properties.UrlResource.BaseUrl, parameters);
        }
        /// <summary>
        /// GET方式发送特殊微博指令
        /// </summary>
        /// <param name="command">微博命令。如remind/unread_count。详见官方API文档：http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2 </param>
        /// <param name="parameters">参数表</param>
        /// <returns></returns>
        internal async Task<string> GetSpecialCommandAsync(string command, params WeiboParameter[] parameters) {
            return await HttpAsync(command, HttpRequestMethod.Get, Properties.UrlResource.SpecialUrl, parameters);
        }
        /// <summary>
        /// POST方式发送特殊微博指令
        /// </summary>
        /// <param name="command">微博命令。如remind/set_count。详见官方API文档：http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2 </param>
        /// <param name="parameters">参数表</param>
        /// <returns></returns>
        internal async Task<string> PostSpecialCommandAsync(string command, params WeiboParameter[] parameters) {
            return await HttpAsync(command, HttpRequestMethod.Post, Properties.UrlResource.SpecialUrl, parameters);
        }
    }
}
